package org.msfj.admin.data.service.impl;

import cn.hutool.core.util.StrUtil;
import org.msfj.admin.data.service.MessageFiledService;
import org.msfj.admin.model.MessageFiled;
import org.msfj.admin.vo.MessageFiledVo;
import org.msfj.admin.data.service.MessageFiledService;
import org.msfj.admin.model.MessageFiled;
import org.msfj.admin.vo.MessageFiledVo;
import org.msfj.admin.data.service.MessageFiledService;
import org.msfj.admin.model.MessageFiled;
import org.msfj.admin.vo.MessageFiledVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统短信字段信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-24 16:05:31
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "messageFiled")
public class MessageFiledServiceImpl extends BaseServiceImpl<MessageFiled> implements MessageFiledService {

    /**
     * 根据条件分页查询系统短信字段信息列表
     *
     * @param messageFiledVo
     * @param page           当前第几页
     * @param rows           每页多少条记录
     * @return PageInfo<MessageFiled>
     */
    @Override
    //@Cacheable
    public PageInfo<MessageFiledVo> getByMessageFiled(MessageFiledVo messageFiledVo, int page, int rows) {
        Example example = new Example(MessageFiled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        if (messageFiledVo != null) {
            if (StrUtil.isNotBlank(messageFiledVo.getName())) {
                criteria.andLike("name", "%".concat(messageFiledVo.getName()).concat("%"));
            }
        }
        PageHelper.startPage(page, rows);
        List<MessageFiled> messageFiledList = super.getByExample(example);
        PageInfo messageFiledVoPageInfo = new PageInfo(messageFiledList);
        messageFiledVoPageInfo.setList(setOtherProperties(messageFiledList));
        return messageFiledVoPageInfo;
    }

    /**
     * 根据条件查询所有系统短信字段信息对象列表
     *
     * @param messageFiledVo
     * @return List<MessageFiled>
     */
    @Override
    //@Cacheable
    public List<MessageFiledVo> getAllMessageFiled(MessageFiledVo messageFiledVo) {
        Example example = new Example(MessageFiled.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<MessageFiledVo> messageFiledVoList = setOtherProperties(super.getByExample(example));
        return messageFiledVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param messageFiled
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public MessageFiledVo setOtherProperties(MessageFiled messageFiled) {
        MessageFiledVo messageFiledVo = new MessageFiledVo();
        BeanUtils.copyProperties(messageFiled, messageFiledVo);
        return messageFiledVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param messageFileds
     * @return List<MessageFiledVo>
     */
    @Override
    //@Cacheable
    public List<MessageFiledVo> setOtherProperties(List<MessageFiled> messageFileds) {
        List<MessageFiledVo> messageFiledVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(messageFileds)) {
            for (MessageFiled messageFiled : messageFileds) {
                MessageFiledVo messageFiledVo = new MessageFiledVo();
                BeanUtils.copyProperties(messageFiled, messageFiledVo);
                messageFiledVos.add(messageFiledVo);
            }
        }
        return messageFiledVos;
    }
}
